var ns, toast;
var app = new Vue({
    el: '#app',
    data: {
        list: [],
        form: {
            selecttype: 1, //1-单选 2-多选
            fromtype: 0
        },
        selectlist: [],
        canopen: true
    },
    methods: {
        get_data: function() {
            var that = this;
            var options = {};
            options.action = "getmyuserprojectlist";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.list = data.list;
                } else if (err) {
                    that.list = [];
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
            });
        },
        do_open: function(item) {
            var that = this;
            if (!item.isParent) {
                that.do_choose(item);
                return;
            }
            api.sendEvent({
                name: 'do_switch_projecttab',
                extra: {
                    tabindex: 1,
                    parentid: item.id
                }
            });
        },
        do_choose: function(item) {
            var that = this;
            item.isselect = !item.isselect;
            if (that.form.selecttype == 1) {
                for (var i = 0; i < that.list.length; i++) {
                    if (that.list[i].id != item.id) {
                        that.list[i].isselect = false;
                        continue;
                    }
                }
                that.selectlist = [];
                if (item.isselect) {
                    that.selectlist.push(item);
                }
            } else {
                var canadd = true;
                var canremove = false;
                var removeindex = 0;
                for (var i = 0; i < that.selectlist.length; i++) {
                    if (that.selectlist[i] == item.id) {
                        canadd = false;
                        canremove = true;
                        removeindex = i;
                    }
                }
                if (item.isselect) {
                    if (canadd) {
                        that.selectlist.push(item);
                    }
                } else {
                    if (canremove) {
                        that.selectlist.splice(removeindex, 1);
                    }
                }
            }
        },
        do_complete: function() {
            var that = this;
            that.canopen = true;
            that.do_save();
        },
        do_send_event: function() {
            var that = this;
            that.canopen = false;
            that.do_save();
        },
        do_save: function() {
            var that = this;
            if (that.selectlist.length == 0) {
                api.toast({
                    msg: '请选择资源',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            var roomid = 0;
            var projectid = 0;
            var name = '';
            var namenum='';
            var roomidlist = [];
            var projectidlist = [];
            var namelist = [];
            if (that.selectlist[0].isParent) {
                projectid = that.selectlist[0].id;
                projectidlist.push(projectid);
            } else {
                roomid = that.selectlist[0].id;
                roomidlist.push(roomid);
            }
            name = that.selectlist[0].fullname;
            namenum=that.selectlist[0].name;
            var options = {};
            options.action = "getprojectheaderlist";
            options.parentid = that.selectlist[0].id;
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    var params = {
                        roomid: roomid,
                        projectid: projectid,
                        roomids: JSON.stringify(roomidlist),
                        projectids: JSON.stringify(projectidlist),
                        name: name,
                        namenum:namenum,
                        headerlist: JSON.stringify(data.headerlist)
                    }
                    if (that.form.fromtype > 0 && that.canopen) {
                        var openurl = '';
                        var title = '';
                        if (that.form.fromtype == 1) {
                            openurl = 'meterpoingwrite_frm';
                            title = '水电抄表';
                        } else if (that.form.fromtype == 2) {
                            openurl = 'meterpublicwrite_frm';
                            title = '公表抄表';
                        } else if (that.form.fromtype == 3) {
                            openurl = 'metersumwrite_frm';
                            title = '总表抄表';
                        } else if (that.form.fromtype == 4) {
                            openurl = 'shoufeiguanli_frm';
                            title = '区域管家';
                        }
                        ns.openWin(openurl, title, params)
                        return;
                    }
                    setTimeout(function() {
                        api.sendEvent({
                            name: 'do_choose_project_complete',
                            extra: params
                        });
                        api.closeWin();
                    }, 500)
                }
            });
        },
        do_add: function() {
            var that = this;
            ns.openWin('chooseprojectlist_frm', '新增常用资源', {
                selecttype: 2,
                isadd: 1,
                fromtype: that.form.fromtype
            });
        },
        do_remove: function(item) {
            var that = this;
            var options = {};
            options.action = "doremovemyproject";
            options.ProjectID = item.id;
            api.confirm({
                title: '提示',
                msg: '确认删除常用资源',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        ns.post(options, function(succeed, data, err) {
                            if (succeed) {
                                that.get_data();
                            } else if (err) {
                                api.toast({
                                    msg: err,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        }, {
                            toast: true,
                            toastmsg: '删除中'
                        });
                    }
                }
            });
        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    // app.form.selecttype = api.pageParam.selecttype || 1;
    app.form.fromtype = api.pageParam.fromtype || 0;

    app.get_data();
    api.addEventListener({
        name: 'do_add_project_complete'
    }, function(ret, err) {
        app.get_data();
    });
    api.addEventListener({
        name: 'getselectprojectids'
    }, function(ret, err) {
        app.do_send_event();
    });

    api.sendEvent({
        name: 'closecpframe'
    });

}
